'use strict';

const Service = require('egg').Service;
var baseUrl = 'http://127.0.0.1:5700';

class SendMsgService extends Service {
    async sendToPerson(params) {

        let id = params.id;
        let msg = params.msg;
        let result = await this.ctx.curl(`${baseUrl}/send_private_msg`, {
            data: {
                user_id: id,
                message: msg,
                auto_escape: false
            },
            timeout: [ 5000, 30000 ],
        });
        result = JSON.parse(result.data.toString());
        // console.log(result);
        if (result.status == 'ok') {
            console.log(`发送到人成功 id:${result.data.message_id}`);
        }else{
            console.log(`发送到人失败`);
        }
    }

    async sendToGroup(params){
        let id = params.id;
        let msg = params.msg;
        let result = await this.ctx.curl(`${baseUrl}/send_group_msg`, {
            data: {
                group_id: id,
                message: msg,
                auto_escape: false
            },
            timeout: [ 5000, 30000 ],
        });
        result = JSON.parse(result.data.toString());
        // console.log(result);
        if (result.status == 'ok') {
            console.log(`发送到群成功 id:${result.data.message_id}`);
        }else{
            console.log(`发送到群失败`);
        }
    }

    async revoc(id){
        let result = await this.ctx.curl(`${baseUrl}/delete_msg`, {
            data: {
              message_id: id
            }
        })
        result = JSON.parse(result.data.toString());
        if (result.status == 'ok') {
            console.log('撤回成功');
            return true;
        }else{
            console.log('撤回失败');
            return false;
        }
    }
}


module.exports = SendMsgService;